// sidebar AHAH functions (from original code by Phil Ballard)                                                           

// get tag cloud
function showTagCloud() {
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     req = new XMLHttpRequest();
     } catch(e) {
	req = false;
     }
     req.onreadystatechange = function() {
     	returnTimeline();
     };
     req.open("GET",'includes/displaytagcloud.php',true);
     req.send(null);
  }

// get public, friends, inbox, or sent timeline
function getTimeline(timeline,page,preload) {
     if(preload!='no') {
       document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     }
     try {
     req = new XMLHttpRequest();
     } catch(e) {
	req = false;
     }
     req.onreadystatechange = function() {
     	returnTimeline();
     };
     req.open("GET",'includes/displaytimeline.php?timeline='+timeline+'&page='+page,true);
     req.send(null);
  }

// get thread timeline
function getThreadTimeline(video_id,page) {
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     req = new XMLHttpRequest();
     } catch(e) {
	req = false;
     }
     req.onreadystatechange = function() {
     	returnTimeline();
     };
     req.open("GET",'includes/displaytimeline.php?timeline=thread&video_id='+video_id+'&page='+page,true);
     req.send(null);
  }

// get user timeline
function getUserTimeline(username,page) {
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     req = new XMLHttpRequest();
     } catch(e) {
	req = false;
     }
     req.onreadystatechange = function() {
     	returnTimeline();
     };
     req.open("GET",'includes/displaytimeline.php?timeline=user&username='+username+'&page='+page,true);
     req.send(null);
  }

function returnTimeline() {
   var output = '';
   if(req.readyState == 4) {
      document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
      if(req.status == 200) {
         output = req.responseText;
         document.getElementById('show').innerHTML = output;
      } else {
         document.getElementById('show').innerHTML += 'Error:\n'+output;
         }
      }
  }

// toggle Qik videos
function toggleQik() {
     try {
     qreq = new XMLHttpRequest();
     } catch(e) {
	qreq = false;
     }
     qreq.onreadystatechange = function() {
     	returnToggleQik();
     };
     qreq.open("GET",'includes/toggleqik.php',true);
     qreq.send(null);
  }

function returnToggleQik() {
   var output = '';
   if(qreq.readyState == 4) {
      if(qreq.status == 200) {
         output = qreq.responseText;
         document.getElementById('qik').innerHTML = output;
      } else {
         document.getElementById('qik').innerHTML += 'Error:\n'+output;
         }
      }
  }

// get tags
function getTags(video_id,thisAuthor) {
     document.getElementById('tags').innerHTML = '<p>retrieving tags - please wait...</p>';
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     treq = new XMLHttpRequest();
     } catch(e) {
	treq = false;
     }
     treq.onreadystatechange = function() {
     	returnGetTags();
     };
     
     treq.open("GET",'includes/displaytags.php?video_id='+video_id+'&thisAuthor='+thisAuthor,true);
     treq.send(null);
  }

function returnGetTags() {
	var output = '';
	if(treq.readyState == 4) {
		document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
		if(treq.status == 200) {
			output = treq.responseText;
			document.getElementById('tags').innerHTML = output;
		} else {
			document.getElementById('tags').innerHTML += 'Error:\n'+output;
		}
	}
}

// add a tag
function addTag(video_id,tagInput,thisAuthor) {
     document.getElementById('tags').innerHTML = '<p>adding tag \"'+tagInput+'\" - please wait...</p>';
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     utreq = new XMLHttpRequest();
     } catch(e) {
	utreq = false;
     }
     utreq.onreadystatechange = function() {
     	returnAddTag(video_id,tagInput,thisAuthor);
     };
     
     utreq.open("GET",'includes/addtag.php?video_id='+video_id+'&tagInput='+tagInput,true);
     utreq.send(null);
  }

function returnAddTag(video_id,tagInput,thisAuthor) {
   var output = '';
	if(utreq.readyState == 4) {
		document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
		if(utreq.status == 200) {
		        document.getElementById('tags').innerHTML = '<p>added tag \''+tagInput+'\'</p>';
			getTags(video_id,thisAuthor);
		} else {
			document.getElementById('tags').innerHTML += 'Error:\n'+output;
		}
	}
}

// remove a tag
function removeTag(video_id,tagToRemove,thisAuthor) {
     document.getElementById('tags').innerHTML = '<p>removing tag \"'+tagToRemove+'\" - please wait...</p>';
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     rtreq = new XMLHttpRequest();
     } catch(e) {
	rtreq = false;
     }
     rtreq.onreadystatechange = function() {
     	returnRemoveTag(video_id,tagToRemove,thisAuthor);
     };
     
     rtreq.open("GET",'includes/removetag.php?video_id='+video_id+'&tagToRemove='+tagToRemove,true);
     rtreq.send(null);
  }

function returnRemoveTag(video_id,tagToRemove,thisAuthor) {
   var output = '';
	if(rtreq.readyState == 4) {
		document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
		if(rtreq.status == 200) {
		        document.getElementById('tags').innerHTML = '<p>tag \''+tagToRemove+'\' removed</p>';
			getTags(video_id,thisAuthor);
		} else {
			document.getElementById('tags').innerHTML += 'Error:\n'+output;
		}
	}
}

// get user profile
function getUserProfile(username) {
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     ureq = new XMLHttpRequest();
     } catch(e) {
	ureq = false;
     }
     ureq.onreadystatechange = function() {
     	returnUserProfile();
     };
     ureq.open("GET",'includes/displayuser.php?username='+username,true);
     ureq.send(null);
  }

function returnUserProfile() {
   var output = '';
   if(ureq.readyState == 4) {
      document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
      if(ureq.status == 200) {
         output = ureq.responseText;
         document.getElementById('player').innerHTML = output;
      } else {
         document.getElementById('player').innerHTML += 'Error:\n'+output;
         }
      }
  }

// get search
function getSearch(query,page) {
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     sreq = new XMLHttpRequest();
     } catch(e) {
	sreq = false;
     }
     sreq.onreadystatechange = function() {
     	returnSearch();
     };
     sreq.open("GET",'includes/displaysearch.php?query='+query+'&page='+page,true);
     sreq.send(null);
  }

function returnSearch() {
   var output = '';
   if(sreq.readyState == 4) {
      document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
      if(sreq.status == 200) {
         output = sreq.responseText;
         document.getElementById('show').innerHTML = output;
      } else {
         document.getElementById('show').innerHTML += 'Error:\n'+output;
         }
      }
  }

// show tag search videos
function showTagVideos(tagsearch,page) {
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     streq = new XMLHttpRequest();
     } catch(e) {
	streq = false;
     }
     streq.onreadystatechange = function() {
     	returnShowTagVideos();
     };
     streq.open("GET",'includes/displaytagvideos.php?tagsearch='+tagsearch+'&page='+page,true);
     streq.send(null);
  }

function returnShowTagVideos() {
   var output = '';
   if(streq.readyState == 4) {
      document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
      if(streq.status == 200) {
         output = streq.responseText;
         document.getElementById('show').innerHTML = output;
      } else {
         document.getElementById('show').innerHTML += 'Error:\n'+output;
         }
      }
  }

// get add link
function getAddLink(video_id,url_thumbnail,title) {
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     alreq = new XMLHttpRequest();
     } catch(e) {
	alreq = false;
     }
     alreq.onreadystatechange = function() {
     	returnAddLink();
     };
     alreq.open("GET",'includes/addlink.php?video_id='+video_id+'&url_thumbnail='+url_thumbnail+'&title='+title,true);
     alreq.send(null);
  }

function returnAddLink() {
   var output = '';
   if(alreq.readyState == 4) {
      document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
      if(alreq.status == 200) {
         output = alreq.responseText;
         document.getElementById('player').innerHTML = output;
      } else {
         document.getElementById('player').innerHTML += 'Error:\n'+output;
         }
      }
  }

// get save link
function getSaveLink(video_id,url) {
     document.getElementById('loadingImage').innerHTML = '<img src=\"images/wait.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" >';
     try {
     slreq = new XMLHttpRequest();
     } catch(e) {
	slreq = false;
     }
     slreq.onreadystatechange = function() {
     	returnSaveLink();
     };
     slreq.open("GET",'includes/savelink.php?video_id='+video_id+'&url='+url,true);
     slreq.send(null);
  }

function returnSaveLink() {
   var output = '';
   if(slreq.readyState == 4) {
      document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"128\" height=\"6\" border=\"0\" vspace=\"1\" />';   
      if(slreq.status == 200) {
         output = slreq.responseText;
         document.getElementById('player').innerHTML = output;
      } else {
         document.getElementById('player').innerHTML += 'Error:\n'+output;
         }
      }
  }

// original functions
function callAHAH(url, pageElement, callMessage, errorMessage) {
     if(callMessage!='nopreload') {
       document.getElementById('loadingImage').innerHTML = callMessage;
     }
     try {
     req = new XMLHttpRequest(); /* e.g. Firefox */
     } catch(e) {
       try {
       req = new ActiveXObject("Msxml2.XMLHTTP");  /* some versions IE */
       } catch (e) {
         try {
         req = new ActiveXObject("Microsoft.XMLHTTP");  /* some versions IE */
         } catch (E) {
          req = false;
         } 
       } 
     }
     req.onreadystatechange = function() {responseAHAH(pageElement, errorMessage);};
     req.open("GET",url,true);
     req.send(null);
  }

function responseAHAH(pageElement, errorMessage) {
   var output = '';
   if(req.readyState == 4) {
      document.getElementById('loadingImage').innerHTML = '<img src=\"images/alpha.gif\" width=\"64\" height=\"10\" border=\"0\" vspace=\"1\" />';   
      if(req.status == 200) {
         output = req.responseText;
         document.getElementById(pageElement).innerHTML = output;
      } else {
         document.getElementById(pageElement).innerHTML += errorMessage+"\n"+output;
         }
      }
  }
  